package com.wxq.yaoxia;

import java.util.Calendar;
import org.json.JSONException;
import org.json.JSONObject;
import com.wxq.net.NetClient;
import com.wxq.net.NetWorkThread;
import com.wxq.yaoxia.mainborad.YXMainboradActivity;
import com.wxq.yaoxia.util.YXConstants;
import com.wxq.yaoxia.util.YXUtility;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;


public class YXRegisterActivity extends YXBaseNetActivity {	
	
	CheckBox agreeCheckBox;
	Button registerBtn;
	Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentViewWithTopbar(R.layout.yx_register, R.layout.yx_topbar);
		agreeCheckBox = (CheckBox) findViewById(R.id.agreeCheckBox);
		registerBtn =(Button) findViewById(R.id.registerBtn);
		registerBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//获得网络数据
				NetWorkThread netWorkThread = new NetWorkThread(YXRegisterActivity.this, YXRegisterActivity.this, mHandler);
				netWorkThread.start();
			}
		});
	}

	@Override
	public void setTopBarButtonListener() {
		Button leftBtn = (Button) findViewById(R.id.btnLeft);
    	Button rightBtnButton = (Button) findViewById(R.id.btnRight);
    	TextView centerTV = (TextView) findViewById(R.id.centerTV);
    	
    	leftBtn.setVisibility(View.VISIBLE);
    	leftBtn.setText("返回");
    	leftBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
    	rightBtnButton.setVisibility(View.VISIBLE);
//    	rightBtnButton.setText("药");
    	rightBtnButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(YXRegisterActivity.this, YXMainboradActivity.class);
				YXRegisterActivity.this.startActivity(intent);
			}
		});
    	centerTV.setVisibility(View.VISIBLE);
    	centerTV.setText("填写注册信息");

	}
	
	public void registerUser() {
		String emailaddr = ((EditText)findViewById(R.id.emailET)).getText().toString();
		String disname =  ((EditText)findViewById(R.id.userNameET)).getText().toString();
		String password1 = ((EditText)findViewById(R.id.passwordET1)).getText().toString();
		String password2 = ((EditText)findViewById(R.id.passwordET2)).getText().toString();
		
		
		
		if(TextUtils.isEmpty(emailaddr)||
		   TextUtils.isEmpty(disname)||
		   TextUtils.isEmpty(password1)||
		   TextUtils.isEmpty(password2))
		{
			YXUtility.showToast(this, "请输入完整的注册信息!", mHandler);
			return;
		}
		
		if(!password1.equals(password2))
		{
			YXUtility.showToast(this, "两次输入的密码必须一致!", mHandler);
			return;
		}
		
		if (! agreeCheckBox.isChecked()) {
			YXUtility.showToast(this, "必须同意药侠网使用条款才能注册!", mHandler);
			return;
		}
		
		String timestamp = Calendar.getInstance().getTime().getTime()+""; 
		
		String checksum = YXUtility.encryptToMD5(emailaddr + timestamp + password1 + timestamp + disname + "yaoxia");
		String url = String.format(YXConstants.URL_REGISTER, emailaddr,password1,password2,disname,timestamp,checksum.toLowerCase());
		Log.d("wxq", "url = "+url);
		String result = NetClient.doGet(url);
		Log.d("wxq", "result = "+result);
		if(TextUtils.isEmpty(result))
		{
			YXUtility.showToast(this, "Get data erro,please check your network!", mHandler);
			return ;
		}
		try {
			JSONObject resultJSON = new JSONObject(result);
			int code = resultJSON.getInt("code");
			String msg = resultJSON.getString("message");
			if(code == 500)
			{
				YXUtility.showToast(this, msg, mHandler);
			}
			else if(code == 200)
			{
				YXUtility.showToast(this, msg, mHandler);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void getNetworkData() {
		registerUser();
	}

}
